package edu.cmu.ibill.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import edu.cmu.ibill.databeans.*;
import edu.cmu.ibill.model.*;

public class ShowAccountSummaryAction extends Action {

	UserDAO userDAO;
	PersonalBillDAO personalbillDAO;
	DebtDAO debtDAO;

	public ShowAccountSummaryAction(Model model) {
		this.debtDAO = model.getDebtDAO();
		this.personalbillDAO = model.getPersonalBillDAO();
		this.userDAO = model.getUserDAO();

	}

	@Override
	public String getName() {
		return "showaccountsummary.do";
	}

	@Override
	public String perform(HttpServletRequest request) {
		List<String> errors = new ArrayList<String>();
		request.setAttribute("addpersonalbillerrors", errors);
		User user = (User) request.getSession(false).getAttribute("user");

		if (user == null || user.getId() == -1) {
			request.getSession(false).setAttribute("user", null);
			return "login.jsp";
		}

		int userId = user.getId();

		Debt[] needCollectDebts = debtDAO.LookupByPayedId(userId);
		Debt[] needReturnDebts = debtDAO.LookupByOwedId(userId);

		HashMap<Integer, Double> summary = new HashMap<Integer, Double>();

		double needToCollect = 0;
		double needToReturn = 0;

		for (Debt debt : needCollectDebts) {
			needToCollect += debt.getAmount();
			int owedUserid = debt.getOwedId();
			if (summary.containsKey(owedUserid))
				summary.put(owedUserid,
						summary.get(owedUserid) + debt.getAmount());
			else
				summary.put(owedUserid, debt.getAmount());
		}

		for (Debt debt : needReturnDebts) {
			needToReturn += debt.getAmount();
			int owedUserid = debt.getOwedId();
			if (summary.containsKey(owedUserid))
				summary.put(owedUserid,
						summary.get(owedUserid) - debt.getAmount());
			else
				summary.put(owedUserid, -debt.getAmount());
		}

		HashMap<String, Double> summaryWithNames = new HashMap<String, Double>();

		for (int id : summary.keySet()) {
			String key = userDAO.getNamebyUserID(id) + " ("
					+ userDAO.getUserEmail(id) + ")";
			summaryWithNames.put(key, summary.get(id));
		}

		// The total amount of the money this user need to collect from others.
		request.setAttribute("needToCollect", needToCollect);
		// The total amount of the money this user need to return to others.
		request.setAttribute("needToReturn", needToReturn);
		// The summary of the all the debts this user
		request.setAttribute("debtSummary", summaryWithNames);
		// The monthly income
		request.setAttribute("monthlyIncome",
				personalbillDAO.getCurrentMonthIncome(userId));
		// The monthly expense
		request.setAttribute("monthlyExpense",
				personalbillDAO.getCurrentMonthExpense(userId));

		return "accountsummary.jsp";
	}

	public double getNeedToReturn(User user) {
		Debt[] needReturnDebts = debtDAO.LookupByOwedId(user.getId());

		double needToReturn = 0;

		for (Debt debt : needReturnDebts) {
			needToReturn += debt.getAmount();
		}
		return Math.abs(needToReturn);
	}

	public double getNeedToCollect(User user) {
		Debt[] needCollectDebts = debtDAO.LookupByPayedId(user.getId());

		double needToCollect = 0;

		for (Debt debt : needCollectDebts) {
			needToCollect += debt.getAmount();
		}
		return Math.abs(needToCollect);
	}

	public HashMap<String, Double> getDebtSummary(User user) {
		int userId = user.getId();

		Debt[] needCollectDebts = debtDAO.LookupByPayedId(userId);
		Debt[] needReturnDebts = debtDAO.LookupByOwedId(userId);

		HashMap<Integer, Double> summary = new HashMap<Integer, Double>();

		for (Debt debt : needCollectDebts) {
			int owedUserid = debt.getOwedId();
			if (summary.containsKey(owedUserid))
				summary.put(owedUserid,
						summary.get(owedUserid) + debt.getAmount());
			else
				summary.put(owedUserid, debt.getAmount());
		}

		for (Debt debt : needReturnDebts) {
			int owedUserid = debt.getOwedId();
			if (summary.containsKey(owedUserid))
				summary.put(owedUserid,
						summary.get(owedUserid) - debt.getAmount());
			else
				summary.put(owedUserid, -debt.getAmount());
		}

		HashMap<String, Double> summaryWithNames = new HashMap<String, Double>();

		for (int id : summary.keySet()) {
			String key = userDAO.getNamebyUserID(id) + " ("
					+ userDAO.getUserEmail(id) + ")";
			summaryWithNames.put(key, summary.get(key));
		}
		return summaryWithNames;
	}

}
